package com.stars.easyms.logger.support.logback.joran;

import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.spi.*;
import com.stars.easyms.logger.support.EasyMsOverride;
import com.stars.easyms.logger.support.logback.joran.action.EasyMsAppenderAction;
import com.stars.easyms.logger.support.logback.joran.spi.EasyMsSimpleRuleStore;
import com.stars.easyms.logger.support.logback.sift.EasyMsSiftAction;
import com.stars.easyms.logger.support.logback.util.EasyMsDefaultNestedComponentRules;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>className: EasyMsJoranConfigurator</p>
 * <p>description: 自定义JoranConfigurator类</p>
 *
 * @author guoguifang
 * @date 2019-08-20 11:29
 * @since 1.3.0
 */
public final class EasyMsJoranConfigurator<E> extends JoranConfigurator implements EasyMsOverride {

    @Override
    public void addInstanceRules(RuleStore rs) {
        super.addInstanceRules(rs);
        rs.addRule(new ElementSelector("configuration/appender"), new EasyMsAppenderAction<E>());
        rs.addRule(new ElementSelector("configuration/appender/sift"), new EasyMsSiftAction());
    }

    @Override
    protected void buildInterpreter() {
        RuleStore rs = new EasyMsSimpleRuleStore(context);
        addInstanceRules(rs);
        this.interpreter = new Interpreter(context, rs, initialElementPath());
        InterpretationContext interpretationContext = interpreter.getInterpretationContext();
        interpretationContext.setContext(context);
        addImplicitRules(interpreter);
        addDefaultNestedComponentRegistryRules(interpretationContext.getDefaultNestedComponentRegistry());
        Map<String, Object> omap = interpretationContext.getObjectMap();
        omap.put(ActionConst.APPENDER_BAG, new HashMap<String, Appender<?>>(32));
    }

    @Override
    protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegistry registry) {
        EasyMsDefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(registry);
    }

}